<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.js"></script>
		<script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="../js/plugins/jquery-form/jquery.form.js"></script>
		<script type="text/javascript">
			$(function () {
			    //转换当前登录用户的数据
				var user ;
                user = JSON.parse(sessionStorage.getItem("user"));
			    //跳转到编辑界面,回显数据
				$(".info").renderValues(user);
				//设置背景图片
				$(".myBg").css("background-image","url('"+user.coverImgUrl+"')");


                //给保存按钮绑定事件，提交表单，发送ajax请求
				$("#saveBtn").click(function () {
					$.ajax({
						url:"/users/"+user.id,
						type:"PUT",
                        data:$("#infoForm").serialize(),
                        success:function (data) {
							if(data.success){
							    //保存成功的同时，因为编辑的时候没有提交到email，接口那边也是直接返回过来的，所以要重新将旧的邮件给到新的用户数据中
								data.data.email=user.email;
								//并且更新到会话对象当中去
								sessionStorage.setItem("user",JSON.stringify(data.data));
								//装b用的弹窗
                                $(document).dialog({
                                    type : 'confirm',
                                    style: 'ios',
                                    titleText: "保存成功",
                                    content: "点击确定跳转到个人界面",
                                    buttons: [
                                        {
                                            name: '要跳就点我呀',
                                            callback: function() {
                                                location.href = "/mine/profiles.html"
                                            }
                                        }
                                    ]
                                });
							}else{
                                $(document).dialog({
                                    autoClose: 1500,
                                    content: "保存失败了，原因竟然是："+data.msg
                                });
							}
                        }
					})
                });

				var flag = true ;

				//找到头像的编辑按钮
				$("#headBtn,#headImg").click(function () {
				    flag = false ;
					//找到文件上传器
					$("#imgForm input[name='pic']").click();
                })
				//找到背景图的编辑按钮
				$("#coverBtn").click(function () {
                    flag = true ;
                    //找到文件上传器
                    $("#imgForm input[name='pic']").click();
                })

				//给文件上传框绑定状态改变事件
                $("#imgForm input[name='pic']").change(function () {
                    //判断是否有选择到文件
                    if ($("#imgForm input[name='pic']").val()) {
                        //提交请求
                        $("#imgForm").ajaxSubmit(function (data) {
                            if(flag){
                                //设置背景图片
                                $(".myBg").css("background-image","url('"+data.url+"')");
                                //把背景的路径保存到表单的隐藏域当中
                                $("#infoForm input[name='coverImgUrl']").val(data.url);
                            }else{
                                $("#headImg").attr("src",data.url);
                                //把头像的路径保存到表单的隐藏域当中
                                $("#infoForm input[name='headImgUrl']").val(data.url);
                            }
                        })
                    }
                })


            });
		</script>

	</head>

	<body>
		<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" name="pic" style="display: none;" accept="image/*"/>
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a role="button" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" >
					<i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i>
				</div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
				<input type="hidden" name="headImgUrl" render-value="headImgUrl">
				<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
				<label>昵称</label>
				<input type="text" name="nickName" class="form-control" render-value="nickName">
				<label>性别</label>
				<select class="form-control" name="gender" render-value="gender">
					<option value="0">保密</option>
					<option value="1">男</option>
					<option value="2">女</option>
				</select>
				<label>地区</label>
				<input type="text" name="place"  class="form-control" render-value="place">
				<label>签名</label>
				<textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>

	</body>
</html>
